package aeropuertos.intentos;

import java.util.ArrayList;
import java.util.Date;

import aeropuertos.intent.R;
import android.util.Log;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;



public class PracticaActivity extends ListActivity {
    /** Called when the activity is first created. */
	  private static final String CLASSTAG = PracticaActivity.class.getSimpleName();
	public static final int INSERT_ID = Menu.FIRST;
	public static final int ACTIVITY_ADD = 0;
	
	
	 private BaseDatos mDbHelper;
	    private Cursor mNotesCursor;
	@Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.v(Constants._COUNT, " " + PracticaActivity.CLASSTAG + " onCreate");
        

        // NOTE* This Activity MUST contain a ListView named "@android:id/list"
        // (or "list" in code) in order to be customized
        // http://code.google.com/android/reference/android/app/ListActivity.html

        mDbHelper = new BaseDatos(this);
        mDbHelper.open();
       
        Log.v("practica Activity", "inicia base de datos"); //v para verbose 
        
        fillData();
        Log.v("practica Activity", "inicia pantalla");
        registerForContextMenu(getListView());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.Add);
        return result;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case INSERT_ID:
        Intent intent = new Intent(this, Pantalla_Add.class);
        
        startActivityForResult(intent, ACTIVITY_ADD);
      
        return true;
    }
    return super.onOptionsItemSelected(item);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
       
        switch(requestCode) {
            case ACTIVITY_ADD:
            	
            	mDbHelper.createNote(extras.getString("code"),extras.getString("country"),extras.getString("fecha"));
       
            	 fillData();
                

            	break;
        }
    }
    private void fillData() {
        // Get all of the rows from the database and create the item list
        mNotesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(mNotesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from1 = new String[]{BaseDatos.CODE,BaseDatos.COUNTRY,BaseDatos.FECHA};
      
      

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to1 = new int[]{R.id.text1,R.id.text2,R.id.text3};
        
        

        // Now create a simple cursor adapter and set it to display
       
        CursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.row, mNotesCursor, from1, to1);
        setListAdapter(notes);
       
       
    }
    public void onPause(){
    	super.onPause();
    }
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
}
